home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / misc / memtest8.000 / memtest8 / memtest86 / build.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-25  |  5.1 KB  |  210 lines

  1. /*
  2.  *  linux/tools/build.c
  3.  *
  4.  *  Copyright (C) 1991, 1992  Linus Torvalds
  5.  */
  6.  
  7. /*
  8.  * This file builds a disk-image from three different files:
  9.  *
  10.  * - bootsect: max 510 bytes of 8086 machine code, loads the rest
  11.  * - setup: max 4 sectors of 8086 machine code, sets up system parm
  12.  * - test: 80386 code for actual system
  13.  *
  14.  * It does some checking that all files are of the correct type, and
  15.  * just writes the result to stdout, removing headers and padding to
  16.  * the right amount. It also writes some system data to stderr.
  17.  */
  18.  
  19. /*
  20.  * 1-Jan-96 - Modified by Chris Brady to create a bootable MemTest-86 image.
  21.  * Mostly just ripped out code for root device checks.
  22.  */
  23.  
  24. #include <stdio.h>    /* fprintf */
  25. #include <string.h>
  26. #include <stdlib.h>    /* contains exit */
  27. #include <sys/types.h>    /* unistd.h needs this */
  28. #include <sys/stat.h>
  29. #include <sys/sysmacros.h>
  30. #include <unistd.h>    /* contains read/write */
  31. #include <fcntl.h>
  32. #include <linux/a.out.h>
  33. #include "mtest.h"
  34.  
  35. #define MINIX_HEADER 32
  36. #define N_MAGIC_OFFSET 1024
  37. static int GCC_HEADER = sizeof(struct exec);
  38.  
  39. /* max nr of sectors of setup: don't change unless you also change
  40.  * bootsect etc */
  41.  
  42. #define STRINGIFY(x) #x
  43.  
  44. typedef union {
  45.     long l;
  46.     short s[2];
  47.     char b[4];
  48. } conv;
  49.  
  50. long intel_long(long l)
  51. {
  52.     conv t;
  53.  
  54.     t.b[0] = l & 0xff; l >>= 8;
  55.     t.b[1] = l & 0xff; l >>= 8;
  56.     t.b[2] = l & 0xff; l >>= 8;
  57.     t.b[3] = l & 0xff; l >>= 8;
  58.     return t.l;
  59. }
  60.  
  61. short intel_short(short l)
  62. {
  63.     conv t;
  64.  
  65.     t.b[0] = l & 0xff; l >>= 8;
  66.     t.b[1] = l & 0xff; l >>= 8;
  67.     return t.s[0];
  68. }
  69.  
  70. void die(char * str)
  71. {
  72.     fprintf(stderr,"%s\n",str);
  73.     exit(1);
  74. }
  75.  
  76. void usage(void)
  77. {
  78.     die("Usage: build bootsect setup");
  79. }
  80.  
  81. int main(int argc, char ** argv)
  82. {
  83.     int i,c,id, sz;
  84.     unsigned long sys_size;
  85.     char buf[1024];
  86.     struct exec *ex = (struct exec *)buf;
  87.     char major_root, minor_root;
  88.     struct stat sb;
  89.  
  90.     if (argc < 3) {
  91.         usage();
  92.     }
  93.     for (i=0;i<sizeof buf; i++) buf[i]=0;
  94.     if ((id=open(argv[1],O_RDONLY,0))<0)
  95.         die("Unable to open 'boot'");
  96.     if (read(id,buf,MINIX_HEADER) != MINIX_HEADER)
  97.         die("Unable to read header of 'boot'");
  98.     if (((long *) buf)[0]!=intel_long(0x04100301))
  99.         die("Non-Minix header of 'boot'");
  100.     if (((long *) buf)[1]!=intel_long(MINIX_HEADER))
  101.         die("Non-Minix header of 'boot'");
  102.     if (((long *) buf)[3] != 0)
  103.         die("Illegal data segment in 'boot'");
  104.     if (((long *) buf)[4] != 0)
  105.         die("Illegal bss in 'boot'");
  106.     if (((long *) buf)[5] != 0)
  107.         die("Non-Minix header of 'boot'");
  108.     if (((long *) buf)[7] != 0)
  109.         die("Illegal symbol table in 'boot'");
  110.     i=read(id,buf,sizeof buf);
  111.     fprintf(stderr,"Boot sector %d bytes.\n",i);
  112.     if (i != 512)
  113.         die("Boot block must be exactly 512 bytes");
  114.     i=write(1,buf,512);
  115.     if (i!=512)
  116.         die("Write call failed");
  117.     close (id);
  118.     
  119.     if ((id=open(argv[2],O_RDONLY,0))<0)
  120.         die("Unable to open 'setup'");
  121.     if (read(id,buf,MINIX_HEADER) != MINIX_HEADER)
  122.         die("Unable to read header of 'setup'");
  123.     if (((long *) buf)[0]!=intel_long(0x04100301))
  124.         die("Non-Minix header of 'setup'");
  125.     if (((long *) buf)[1]!=intel_long(MINIX_HEADER))
  126.         die("Non-Minix header of 'setup'");
  127.     if (((long *) buf)[3] != 0)
  128.         die("Illegal data segment in 'setup'");
  129.     if (((long *) buf)[4] != 0)
  130.         die("Illegal bss in 'setup'");
  131.     if (((long *) buf)[5] != 0)
  132.         die("Non-Minix header of 'setup'");
  133.     if (((long *) buf)[7] != 0)
  134.         die("Illegal symbol table in 'setup'");
  135.     for (i=0 ; (c=read(id,buf,sizeof buf))>0 ; i+=c )
  136.         if (write(1,buf,c)!=c)
  137.             die("Write call failed");
  138.     if (c != 0)
  139.         die("read-error on 'setup'");
  140.     close (id);
  141.     if (i > SETUPSECS*512)
  142.         die("Setup exceeds " STRINGIFY(SETUPSECS)
  143.             " sectors - rewrite build/boot/setup");
  144.     fprintf(stderr,"Setup is %d bytes.\n",i);
  145.     for (c=0 ; c<sizeof(buf) ; c++)
  146.         buf[c] = '\0';
  147.     while (i<SETUPSECS*512) {
  148.         c = SETUPSECS*512-i;
  149.         if (c > sizeof(buf))
  150.             c = sizeof(buf);
  151.         if (write(1,buf,c) != c)
  152.             die("Write call failed");
  153.         i += c;
  154.     }
  155.     
  156.     if ((id=open(argv[3],O_RDONLY,0))<0)
  157.         die("Unable to open 'setup'");
  158. #ifndef ELF
  159.     if (read(id,buf,GCC_HEADER) != GCC_HEADER)
  160.         die("Unable to read header of 'test'");
  161.         if (N_MAGIC(*ex) == ZMAGIC) {
  162.                 GCC_HEADER = N_MAGIC_OFFSET;
  163.                 lseek(id, GCC_HEADER, SEEK_SET);
  164.         } else if (N_MAGIC(*ex) != QMAGIC)
  165.                 die("Non-GCC header of 'test'");
  166.     fprintf(stderr,"Test is %d kB (%d kB code, %d kB data and %d kB bss)\n",
  167.         (ex->a_text+ex->a_data+ex->a_bss)/1024,
  168.         ex->a_text /1024,
  169.         ex->a_data /1024,
  170.         ex->a_bss  /1024);
  171.     sz = N_SYMOFF(*ex) - GCC_HEADER + 4;
  172. #else
  173.         if (fstat (id, &sb)) {
  174.           perror ("fstat");
  175.           die ("Unable to stat 'test'");
  176.         }
  177.         sz = sb.st_size;
  178.         fprintf (stderr, "Test is %d kB\n", sz/1024);
  179.     sys_size = (sz + 15) / 16;
  180.     if (sys_size > TSTSIZE)
  181.         die("Test is too big");
  182. #endif
  183.     while (sz > 0) {
  184.         int l, n;
  185.  
  186.         l = sz;
  187.         if (l > sizeof(buf))
  188.             l = sizeof(buf);
  189.         if ((n=read(id, buf, l)) != l) {
  190.             if (n == -1) 
  191.                 perror(argv[1]);
  192.             else
  193.                 fprintf(stderr, "Unexpected EOF\n");
  194.             die("Can't read 'test'");
  195.         }
  196.         if (write(1, buf, l) != l)
  197.             die("Write failed");
  198.         sz -= l;
  199.     }
  200.  
  201.     close(id);
  202.     if (lseek(1,500,0) == 500) {
  203.         buf[0] = (sys_size & 0xff);
  204.         buf[1] = ((sys_size >> 8) & 0xff);
  205.         if (write(1, buf, 2) != 2)
  206.             die("Write failed");
  207.     }
  208.     return(0);
  209. }
  210.